﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using SohuWeb.Models;

namespace SohuWeb.Controllers
{
    public class CmsController : Controller
    {
        //
        // GET: /Cms/
        [Authorize]
        public ActionResult CSVUpload()
        {
            return View();
        }

        //
        // GET: /Cms/Details/5
        [Authorize]
        [HttpPost]
        public ActionResult CSVUpload(string val)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "csv\\";
            string filename = Path.GetFileName(Request.Files["CSVFileName"].FileName);
            if (!filename.EndsWith(".csv")) {
                ModelState.AddModelError("", "您只能上传扩展名为.csv的文件");
                return View();
            }
            Request.Files["CSVFileName"].SaveAs(Path.Combine(path, filename));
            return RedirectToAction("CSVUploadSuccess");
        }

        public ActionResult CSVUploadSuccess() {
            return View();
        }

        [Authorize]
        public ActionResult Config() {
            Config conf = new Config();
            ViewData["CSVPath"] = conf.CSVPath;
            if (conf.CSVGatherTime.Length == 4) {
                ViewData["CSVGatherTimeHour"] = conf.CSVGatherTime.Substring(0, 2);
                ViewData["CSVGatherTimeMinute"] = conf.CSVGatherTime.Substring(2, 2);
            }
            if (conf.NewDataGatherTime.Length == 4) {
                ViewData["NewDataGatherTimeHour"] = conf.NewDataGatherTime.Substring(0, 2);
                ViewData["NewDataGatherTimeMinute"] = conf.NewDataGatherTime.Substring(2, 2);
            }
            if (conf.ComputeTime.Length == 4) {
                ViewData["ComputeTimeHour"] = conf.ComputeTime.Substring(0, 2);
                ViewData["ComputeTimeMinute"] = conf.ComputeTime.Substring(2, 2);
            }
            
            return View();
        }
        
        [Authorize]
        [HttpPost]
        public ActionResult Config(ConfigModel model) {
            bool hasError = false;
            if (model.CSVPath == null) {
                ModelState.AddModelError("", "必须指定CSV保存路径");
                hasError = true;
            } else if (!Directory.Exists(model.CSVPath)) {
                ModelState.AddModelError("", "指定的路径不存在，请重新输入");
                hasError = true;
            }

            if (hasError) {
                Config conf = new Config();
                ViewData["CSVPath"] = conf.CSVPath;
                ViewData["CSVGatherTimeHour"] = conf.CSVGatherTime.Substring(0, 2);
                ViewData["CSVGatherTimeMinute"] = conf.CSVGatherTime.Substring(2, 2);
                ViewData["NewDataGatherTimeHour"] = conf.NewDataGatherTime.Substring(0, 2);
                ViewData["NewDataGatherTimeMinute"] = conf.NewDataGatherTime.Substring(2, 2);
                ViewData["ComputeTimeHour"] = conf.ComputeTime.Substring(0, 2);
                ViewData["ComputeTimeMinute"] = conf.ComputeTime.Substring(2, 2);
                return View();
            } else {
                Config conf = new Config();
                conf.CSVPath = model.CSVPath;
                conf.CSVGatherTime = model.CSVGatherTimeHour + model.CSVGatherTimeMinute;
                conf.NewDataGatherTime = model.NewDataGatherTimeHour + model.NewDataGatherTimeMinute;
                conf.ComputeTime = model.ComputeTimeHour + model.ComputeTimeMinute;
                conf.SaveConfig();
            }
            return RedirectToAction("ConfigChanged");
        }

        public ActionResult ConfigChanged() {
            return View();
        }

        [Authorize]
        public ActionResult ShowAttendance(int pageno, string skey, string svalue) {
            ViewData["TableHeader"] = "序号,卡号,编号,姓名,部门,批次,日期,密集点,起始时间,0点,1点,2点,3点,4点,5点,6点,7点,8点,9点,10点,11点, 12点,13点,14点,15点,16点,17点,18点,19点,20点,21点,22点,23点,最早,最晚";
            return View();
        }
    }
}
